/**
 * 用户模块
 */
define(function(require, exports, module) {
    "require:nomunge,exports:nomunge,module:nomunge";
    "use strict";

    var app = require("mokit/app");
    var service = require('./service');
    var store = require('mokit/store');

    var stateCacheKey = 'user/state';

    var self = {
        /**
         * 用户账号
         */
        account: 'lkt3',//'admin',

        /**
         * 用户密码
         */
        password: '1',//'admin',

        /**
         * 获取当前用户状态
         */
        getState: function() {
            if (self.state == null) {
                self.state = store.local.get(stateCacheKey) || {};
            }
            return self.state;
        },

        /**
         * 保存当前用户状态
         */
        setState: function(state) {
            self.state = state || {};
            store.local.set(stateCacheKey, state);
        },

        /**
         * 登录
         */
        login: function(callback) {
            var self = this;
            var device = window.device || {};
            device.platform = device.platform || 'unknow';
            device.version = device.version || 'unknow';
            device.uuid = device.uuid || 'unknow';
            var loginInfo = {
                account: self.account,
                password: self.password,
                device: device.platform + ' ' + device.version,
                deviceId: device.uuid
            };
            service.user.login(loginInfo, function(rs, err) {
                if (err && callback) return callback(err.message || err);
                if (rs.success) {
                    self.setState(rs.obj || {});
                    if (callback) callback();
                } else {
                    rs = rs || {};
                    var msg = rs.msg || 'unknow error';
                    if (callback) callback(msg);
                }
            });
        },

        reg: function(regInfo, callback) {
            var self = this;
            service.user.reg(regInfo, function(rs, err) {
                if (err && callback) {
                    return callback(err.message || err);
                }
                if (rs.success) {
                    if (callback) callback();
                } else {
                    rs = rs || {};
                    var msg = rs.msg || 'unknow error';
                    if (callback) callback(msg);
                }
            });
        },

        /**
         * 登出
         */
        logout: function(callback) {
            service.user.logout(function(rs, err) {
                self.setState(null);
                if (err && callback) {
                    return callback(err.message || err);
                }
                if (rs.state === service.stateCode.success) {
                    if (callback) callback();
                } else {
                    rs = rs || {};
                    var msg = rs.msg || 'unknow error';
                    if (callback) callback(msg);
                }
            });
        }
    };

    return self;
});